﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace Dhgms.TplHelper.Model.NoResultExecutor
{
    public abstract class SqlNonQueryExecutor<TDatabaseHelperClass, TParameterClass, TParameterCollection> : INoResultExecutor
        where TDatabaseHelperClass : Dhgms.DataManager.Model.Helper.Database.DatabaseHelperBase<TParameterClass, TParameterCollection>, new()
        where TParameterClass : DbParameter
        where TParameterCollection : DbParameterCollection
    {
        private readonly string connectionString;

        public SqlNonQueryExecutor(string connectionString)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException("connectionString");
            }

            this.connectionString = connectionString;
        }

        /// <summary>Gets the SQL parameters.</summary>
        public abstract TParameterClass[] SqlParameters { get; }

        /// <summary>Gets the SQL command.</summary>
        public abstract string SqlCommand { get; }

        /// <summary>Gets a value indicating whether to assume the SQL is safe.</summary>
        public virtual bool AssumeSqlSafe
        {
            get
            {
                return false;
            }
        }

        public void Execute()
        {
            new TDatabaseHelperClass().ProcessNonQuery(this.connectionString, this.SqlCommand, this.SqlParameters, this.AssumeSqlSafe);
        }
    }
}
